package conf

import (
	"gitee.com/slclub/aliyun-oss/utils"
	"path/filepath"
	"reflect"
)

var Config struct {
	OSS    OSSConf
	Log    LogConf
	Server ServerConf
}

type OSSConf struct {
	BucketName      string `toml:"bucket_name"`
	EndPoint        string `toml:"end_point"`
	UploadEndPoint  string `toml:"upload_end_point"`
	AccessKeyId     string `toml:"access_key_id"`
	AccessKeySecret string `toml:"access_key_secret"`
	RegionId        string `toml:"region_id"`
	Scheme          string `toml:"scheme"`
	Bucket          string `toml:"bucket"`
	RoleArn         string `toml:"role_arn"`
	SessionName     string `toml:"session_name"`
}

type LogConf struct {
	// log config
	LogLevel   int    `toml:"log_level"`
	Debug      bool   `toml:"debug"`
	LogRelPath string `toml:"log_rel_path"`
	LogAbsPath string `toml:"log_abs_path"`
	Stderr     bool   `toml:"stderr"`
	LogHeader  string `toml:"log_header"` // 日志初始头信息
	LogName    string `toml:"log_file"`   // 日志名称
}

type ServerConf struct {
	Addr  string `toml:"addr"`
	Debug bool   `toml:"debug"`
}

// 用于修改
func (this *OSSConf) Bind(bind map[string]string) {
	this.bindWithKey(bind, "Bucket")
	this.bindWithKey(bind, "AccessKeyId")
	this.bindWithKey(bind, "AccessKeySecret")
	this.bindWithKey(bind, "RoleArn")
	this.bindWithKey(bind, "SessionName")
}

func (this *OSSConf) bindWithKey(bind map[string]string, key string) {
	val, ok := bind["AccessKeySecret"]
	if !ok {
		return
	}
	s := reflect.ValueOf(this).Elem()
	value := reflect.ValueOf(val)
	s.FieldByName(key).Set(value)
}

func init() {
	conf_file := filepath.Join(utils.APP_PATH, "conf/config.toml")
	utils.ReadToml(conf_file, &Config)
}
